<template>
  <div class="container">
    <el-card>
      <el-form  :inline="true" :model="data">
        <el-form-item label="学科名称">
          <el-input placeholder="" v-model="data.subjectName"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="clear" >清空</el-button>
          <el-button type="primary" @click="search">搜索</el-button>
        </el-form-item>
        <el-button type="success" @click="handleAdd" style="float: right" icon="el-icon-edit"
          >新增学科</el-button
        >
      </el-form>
      <!-- 提示 -->
      <el-alert :closable="false" show-icon type="info">总条数共{{total}}条
      </el-alert>
      <el-table :data="nodeList" style="margin:10px; width: 100%">
        <!-- 序号 -->
        <el-table-column  label="序号"  type="index" :index="indexMethod">
        </el-table-column>
        <!-- 学科名称 -->
        <el-table-column prop="subjectName" label="学科名称" width="150">
        </el-table-column>
        <!-- 创建者 -->
        <el-table-column prop="username" label="创建者" width="150">
        </el-table-column>
        <!-- 创建日期 -->
        <el-table-column prop="addDate" label="创建日期" width="150">
          <template #default="{row}">
              {{ formatTime(row.addDate) }}
            </template>
        </el-table-column>
        <!-- 前台是否显示 -->
        <el-table-column prop="isFrontDisplay" label="前台是否显示" width="120" :formatter="handleFormatter">
        </el-table-column>
        <!-- 二级目录 -->
        <el-table-column prop="twoLevelDirectory" label="二级目录" width="120">
        </el-table-column>
        <!-- 标签 -->
        <el-table-column prop="tags" label="标签" width="120">
        </el-table-column>
        <!-- 题目数量 -->
        <el-table-column prop="totals" label="题目数量"> </el-table-column>
        <!-- 操作 -->
        <el-table-column  label="操作" width="240">
          <template #default="{row}">
        <el-button @click="handleClass(row)" type="text" size="small">学科分类</el-button>
        <el-button type="text" size="small" @click="handleTag(row)">学科标签</el-button>
        <el-button type="text" size="small" @click="subClick(row)">修改</el-button>
        <el-button type="text" size="small" @click="handleDel(row)" >删除</el-button>
      </template>
        </el-table-column>
      </el-table>
      <div class="block">
    <!-- <span class="demonstration">分页器——完整功能</span> -->
    <el-pagination
    background
      style="float: right"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="formData.page"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="formData.pagesize"
      layout="  prev, pager, next,sizes,jumper,total"
      :total="total">
    </el-pagination>
  </div>
    </el-card>

    <SubjectsAdd
      :visible.sync = 'visible'
      @closeDialog="closeDialog"
      ref="addRef"
      :detail="detail"
    />

  </div>
</template>

<script>
import { list, remove } from '../../api/hmmm/subjects'
import SubjectsAdd from '../../module-hmmm/components/subjects-add.vue'
import dayjs from 'dayjs'
export default {
  components: {
    SubjectsAdd
  },
  data () {
    return {
      formData: {
        subjectName: '',
        page: 1,
        pagesize: 5
      },
      data: {
        subjectName: ''
      },
      tableData: [],

      nodeList: [],
      total: 0,
      visible: false,

      detail: {}
    }
  },
  created () {
    this.getList()
  },
  methods: {
    formatTime (time) {
      return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
    },
    // 获取列表
    async getList (params) {
      const { data: { items, counts } } = await list(this.formData)
      console.log(items)
      this.nodeList = items
      this.total = counts
    },
    search () {
      this.formData.page = 1
      this.formData.subjectName = this.data.subjectName
      this.getList(this.formData)
    },
    handleFormatter ({ isFrontDisplay }) {
      if (isFrontDisplay === 1) {
        return '是'
      } else if (isFrontDisplay === 0) {
        return '否'
      } else {
        return '未知'
      }
    },
    handleAdd () {
      this.visible = true
      this.$refs.addRef.title = '增加学科'
    },
    subClick (val) {
      console.log(val)
      this.visible = true
      this.detail = val
      this.$refs.addRef.getInfo(val.id)
      this.$refs.addRef.title = '修改'
    },
    handleDel (val) {
      console.log(val)
      this.$confirm('此操作将永久删除该学科, 是否继续?', '提示', {
        type: 'warning'
      }).then(async () => {
        await remove({ id: val.id })
        if (this.nodeList.length === 1 && this.formData.page > 1) {
          this.formData.page--
        }
        this.getList(this.formData)
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    // 每页条数
    handleSizeChange (val) {
      this.formData.pagesize = val
      this.formData.page = 1
      this.getList(this.formData)
    },
    // 页
    handleCurrentChange (val) {
      this.formData.page = val
      this.getList(this.formData)
    },
    // 序号
    indexMethod (index) {
      // console.log(index + 1)
      // return index + 1 + (页码 - 1) * 每页条数
      return index + 1 + (this.formData.page - 1) * this.formData.pagesize
    },
    // 清空
    clear () {
      this.data.subjectName = ''
    },
    handleClass (row) {
      console.log(row.subjectName)
      this.$router.push(`/subjects/directorys?id=${row.id}&name=${row.subjectName}`)
    },
    handleTag (row) {
      console.log(row.subjectName)
      this.$router.push(`/subjects/tags?id=${row.id}&name=${row.subjectName}`)
    },
    closeDialog () {
      if (!this.detail.id) {
        this.formData.page = 1
      }
      this.visible = false
      this.getList()
    }

  }
}
</script>

<style scoped lang='less'>
</style>
